package com.tiancheng.onecode.controller.api.consumer;

import com.tiancheng.onecode.enums.OrderStatusEnum;
import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.model.request.admin.QueryOrderRefundReq;
import com.tiancheng.onecode.model.request.admin.QueryOrderReq;
import com.tiancheng.onecode.model.request.api.PayOrderRequest;
import com.tiancheng.onecode.model.response.PageInfo;
import com.tiancheng.onecode.model.response.Result;
import com.tiancheng.onecode.model.response.admin.QueryOrderRefundRes;
import com.tiancheng.onecode.model.response.admin.QueryOrderRes;
import com.tiancheng.onecode.model.response.api.OrderPayApiRes;
import com.tiancheng.onecode.model.response.api.QueryMerchantApiRes;
import com.tiancheng.onecode.model.response.api.QueryOrderDetailApiRes;
import com.tiancheng.onecode.service.IMemberOrderRefundService;
import com.tiancheng.onecode.service.IMemberOrderService;
import com.tiancheng.onecode.utils.JsonUtil;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName MemberOrderApiController
 * @Description 客户订单-客户端接口
 * @Author kellen
 * @Date 2024/12/14 20:43
 */
@Slf4j
@RequestMapping("/api/c/order")
@RestController
public class MemberOrderApiConsumerController {

    @Resource
    private IMemberOrderService memberOrderService;
    @Resource
    private IMemberOrderRefundService memberOrderRefundService;

    /**
     * 商户信息
     *
     * @param platformMerchantId 平台商户id
     * @return
     */
    @GetMapping("/merchant")
    public Result<QueryMerchantApiRes> merchant(@RequestParam("platformMerchantId") Long platformMerchantId) {
        QueryMerchantApiRes res = memberOrderService.getMerchantInfo(platformMerchantId);
        return Result.success(res);
    }

    /**
     * 下单
     *
     * @param payOrderRequest
     * @return
     */
    @PostMapping("/pay")
    public Result<OrderPayApiRes> pay(@RequestBody @Valid PayOrderRequest payOrderRequest) {
        log.info("接收到支付下单请求：{}", JsonUtil.toJsonString(payOrderRequest));
        OrderPayApiRes payApiRes = memberOrderService.pay(payOrderRequest);
        return Result.success(payApiRes);
    }

    /**
     * 查询订单列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/orderPage", method = RequestMethod.POST)
    @ResponseBody
    public Result<PageInfo<QueryOrderRes>> queryPage(@RequestBody QueryOrderReq params) {
        Asserts.isNotNull(params.getUserId(), "用户id不允许为空！");
        PageInfo<QueryOrderRes> pageInfo = memberOrderService.queryPlatformPage(params);
        if (CollectionUtils.isNotEmpty(pageInfo.getList())) {
            for (QueryOrderRes queryOrderRes : pageInfo.getList()) {
                if (queryOrderRes.getOrderStatus().equals(OrderStatusEnum.paid.getCode()) || queryOrderRes.getOrderStatus().equals(OrderStatusEnum.Completed.getCode())) {
                    queryOrderRes.setOrderStatus(OrderStatusEnum.Completed.getCode());
                }
            }
        }
        return Result.success(pageInfo);
    }

    /**
     * 查询退款单列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/refundPage", method = RequestMethod.POST)
    @ResponseBody
    public Result<PageInfo<QueryOrderRefundRes>> refundPage(@RequestBody QueryOrderRefundReq params) {
        Asserts.isNotNull(params.getUserId(), "用户id不允许为空！");
        PageInfo<QueryOrderRefundRes> pageInfo = memberOrderRefundService.queryPlatformPage(params);
        return Result.success(pageInfo);
    }

    /**
     * 查询订单信息
     *
     * @param orderId 订单号
     * @return
     */
    @GetMapping(value ="/orderDetail/{orderId}")
    @ResponseBody
    public Result<QueryOrderDetailApiRes> queryDetail(@PathVariable("orderId") String orderId) {
        QueryOrderDetailApiRes orderDetail = memberOrderService.queryApiOrderDetail(orderId);
        return Result.success(orderDetail);
    }

    /**
     * 查询订单信息
     *
     * @param refundId 退款单号
     * @return
     */
    @GetMapping(value ="/refundDetail/{refundId}")
    @ResponseBody
    public Result<QueryOrderRefundRes> refundDetail(@PathVariable("refundId") String refundId) {
        QueryOrderRefundRes queryOrderRefundRes = memberOrderRefundService.queryByRefundId(refundId);
        return Result.success(queryOrderRefundRes);
    }

}
